package com.michale.admin.controller.permissions;

import com.michale.admin.controller.tool.BaseController;
import com.michale.common.annotation.Log;
import com.michale.common.constants.UserConstant;
import com.michale.common.domain.permissions.MenuEntity;
import com.michale.common.enmus.BusinessType;
import com.michale.common.result.AjaxResult;
import com.michale.common.utils.String.StringUtils;
import com.michale.system.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Michale
 * @apiNote 菜单管理
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("permissions/menu")
public class MenuController extends BaseController {

    @Resource
    private MenuService menuService;

    /**
     * 获取菜单列表
     *
     * @param menuEntity 菜单信息
     * @return 菜单列表
     */
    @GetMapping("/menuList")
    @ApiOperation(value = "菜单列表", notes = "menuEntity")
    @PreAuthorize("@security.isPermission('permissions:menu:list')")
    public AjaxResult selectMenuList(MenuEntity menuEntity) {
        List<MenuEntity> menuEntityList = menuService.selectMenuList(menuEntity);
        return AjaxResult.success(menuEntityList);
    }

    /**
     * 添加-菜单信息
     *
     * @param menuEntity 菜单信息
     * @return 结果
     */
    @PostMapping("/insertMenu")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PreAuthorize("@security.isPermission('permissions:menu:add')")
    public AjaxResult insertMenu(@Validated @RequestBody MenuEntity menuEntity) {
        if (UserConstant.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menuEntity))) {
            return AjaxResult.error("新增菜单'" + menuEntity.getMenuName() + "'失败，菜单名称已存在");
        } else if (UserConstant.YES_FRAME.equals(menuEntity.getIsFrame()) && !StringUtils.isHttp(menuEntity.getPath())) {
            return AjaxResult.error("新增菜单'" + menuEntity.getMenuName() + "'失败，地址必须以http(s)://开头");
        }
        menuEntity.setCreateBy(getUsername());
        return toAjax(menuService.insertMenu(menuEntity));
    }

    /**
     * 根据菜单ID获取详细信息
     *
     * @param menuId 菜单ID
     * @return 结果
     */
    @GetMapping("/{menuId}")
    @PreAuthorize("@security.isPermission('permissions:menu:query')")
    public AjaxResult menuInfo(@PathVariable("menuId") Long menuId) {
        return AjaxResult.success(menuService.selectMenuById(menuId));
    }

    /**
     * 修改菜单信息
     *
     * @param menuEntity 菜单信息
     * @return 结果
     */
    @PutMapping("/updateMenu")
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@security.isPermission('permissions:menu:edit')")
    public AjaxResult updateMenu(@Validated @RequestBody MenuEntity menuEntity) {
        if (UserConstant.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menuEntity))) {
            return AjaxResult.error("修改菜单'" + menuEntity.getMenuName() + "'失败，菜单名称已存在");
        } else if (UserConstant.YES_FRAME.equals(menuEntity.getIsFrame()) && !StringUtils.isHttp(menuEntity.getPath())) {
            return AjaxResult.error("修改菜单'" + menuEntity.getMenuName() + "'失败，地址必须以http(s)://开头");
        } else if (menuEntity.getMenuId().equals(menuEntity.getParentId())) {
            return AjaxResult.error("修改菜单'" + menuEntity.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        menuEntity.setUpdateBy(getUsername());
        return toAjax(menuService.updateMenu(menuEntity));
    }

    /**
     * 根据菜单ID删除菜单信息
     *
     * @param menuId 需要删除的菜单ID
     * @return 结果
     */
    @DeleteMapping("/{menuId}")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @PreAuthorize("@security.isPermission('permissions:menu:remove')")
    public AjaxResult deleteMenu(@PathVariable("menuId") Long menuId) {
        if (menuService.hasChildByMenuId(menuId)) {
            return AjaxResult.error("存在子菜单,不允许删除");
        }
        if (menuService.checkMenuExistRole(menuId)) {
            return AjaxResult.error("菜单已分配,不允许删除");
        }
        return toAjax(menuService.deleteMenuById(menuId));
    }
}
